<template>
    <view>
        <!-- pages/pointsMallDetail/pointsMallDetail.wxml -->
        <view class="pointsMall">
            <view class="pointsMall-item">
                <image :src="item.imageUrl" mode="widthFix"></image>
                <view class="pointsMall-item-title">{{ item.name }}</view>
                <!-- <view class="pointsMall-item-des"> {{item.classify}} </view> -->
                <view class="pointsMall-item-info">
                    <view class="pointsMall-item-info-num">
                        {{ item.onlineTime }}{{ item.status == 1 ? '上架' : '下架' }}丨已兑换{{ item.soldQuantity || 0 }}件丨剩余{{ item.remainQuantity || 0 }}件
                    </view>
                </view>
                <view class="pointsMall-item-info-points">{{ item.integralAmount }}积分</view>
            </view>
        </view>
        <view class="gift-des">
            <view class="gift-des-title">礼品说明</view>
            <view class="gift-des-info">
                <!-- 解析富文本数据 -->
                <rich-text :nodes="item.description"></rich-text>
            </view>
        </view>
        <view class="btn-box">
            <view class="enter" @tap="goEnter">确定兑换</view>
        </view>
    </view>
</template>

<script>
// pages/pointsMallDetail/pointsMallDetail.js
const app = getApp();

const api = require('../../utils/api.js');

var common = require('../../utils/util.js');

import util from '../../utils/util.js';
export default {
    data() {
        return {
            // item:{title:'蓝月亮洗衣液3kg薰衣草整箱批家用实惠装',des:'护衣护色，低泡易漂',src:'',points:'4699',num:'20111',time:'2021-05-29'},
            item: '',
            giftId: '',
            storeId: '',
            id: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        this.setData({
            giftId: options.giftId
        });
        this.getDetail(options.giftId);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    // onShareAppMessage: function () {
    // }
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */ methods: {
        goEnter() {
            // wx.navigateTo({
            //   url: '../pointsExchange/pointsExchange?giftId='+this.data.giftId,
            // })
            //不去跳转页面，直接调用兑换接口
            uni.showModal({
                title: '',
                content: `本次兑换，将消耗${uni.getStorageSync('phoneNumber')}用户${this.item.integralAmount}积分，是否确认兑换`,
                showCancel: true,
                cancelText: '取消',
                cancelColor: '#999',
                confirmText: '确认',
                confirmColor: '#FFA000',
                success: (res) => {
                    if (res.confirm) {
                        //确认 兑换接口
                        this.integralExchange();
                    } else if (res.cancel) {
                    }
                }
            });
        },

        integralExchange() {
            let param = {
                storeId: this.item.storeId,
                commodityList: [
                    {
                        commodityId: this.item.id,
                        count: 1
                    }
                ]
            };
            uni.$post(api.integralExchange, param).then((res) => {
                if (res.code == 0) {
                    // wx.showToast({ title: "恭喜兑换成功", icon: "none" });
                    uni.showModal({
                        title: '',
                        content: `恭喜兑换成功`,
                        showCancel: false,
                        cancelText: '取消',
                        cancelColor: '#999',
                        confirmText: '确认',
                        confirmColor: '#FFA000',
                        success: (res) => {
                            if (res.confirm) {
                                uni.navigateBack({
                                    delta: 1
                                });
                            } else if (res.cancel) {
                            }
                        }
                    });
                } else {
                    // wx.showToast({ title: res.msg, icon: "none" });
                    uni.showModal({
                        title: '',
                        content: res.msg || `抱歉，兑换失败`,
                        showCancel: false,
                        cancelText: '取消',
                        cancelColor: '#999',
                        confirmText: '我知道了',
                        confirmColor: '#FFA000',
                        success: (res) => {
                            if (res.confirm) {
                            } else if (res.cancel) {
                            }
                        }
                    });
                }
            });
        },

        formatDate(date) {
            var Dates = new Date(date);
            var Y = Dates.getFullYear();
            var M = Dates.getMonth() + 1;
            var D = Dates.getDate();
            var times = Y + (M < 10 ? '-0' : '-') + M + (D < 10 ? '-0' : '-') + D; // return `${date.getFullYear() / date.getMonth() + 1}/${date.getDate()}`;

            return times;
        },

        getDetail(id) {
            let param = {
                id: id
            };
            uni.$post(api.findOne, param)
                .then((res) => {
                    //请求成功
                    if (res.success == true) {
                        var data = res.data;
                        console.log('findOne');
                        var that = this;
                        util.request(api.getEncryPath + '?path=' + data.imageUrl, '', 'POST').then((res) => {
                            data.imageUrl = res;
                            that.setData({
                                item: data
                            });
                        }); // data.putOnTime = this.formatDate(data.putOnTime)
                    } else {
                        this.setData({
                            item: ''
                        });
                        uni.showToast({
                            title: res.msg,
                            icon: 'none'
                        });
                    }
                })
                .catch((err) => {
                    //请求失败
                });
        }
    }
};
</script>
<style>
@import './pointsMallDetail.css';
</style>
